/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.gautoard;

import static com.google.gautoard.OfyService.ofy;
import com.google.gautoard.model.Forecast;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author dras
 */
public class ForecastServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain;charset=UTF-8");
        // <ver>.gautoard.appspot.com/forecast?zipcode=95376&param=high
        // <ver>.gautoard.appspot.com/forecast?zipcode=95376&param=low
        // <ver>.gautoard.appspot.com/forecast?zipcode=95376&param=pop
        // <ver>.gautoard.appspot.com/forecast?zipcode=95376&param=all
        //wget -q -O- "datastore-refactor.gautoard.appspot.com/forecast?zipcode=95376&param=high"

        try (PrintWriter out = response.getWriter()) {
            String zipcode = request.getParameter("zipcode");
            if (zipcode == null || "".equals(zipcode)) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing zipcode");
                return;
            }
            Forecast forecast = ofy().load().type(Forecast.class).id(zipcode).now();
            if (forecast == null) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "I don't have information for that zip code");
                return;
            }
            String param = request.getParameter("param");
            if (null == param || "".equals(param)) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND, "Use a param: {high,low,pop,all}");
                return;
            }
            if ("high".equals(param)) {
                out.println(forecast.getForecastHigh());
            }
            if ("low".equals(param)) {
                out.println(forecast.getForecastLow());
            }
            if ("pop".equals(param)) {
                out.println(forecast.getForecastPop());
            }
            if ("all".equals(param)) {
                out.println("high=" + forecast.getForecastHigh());
                out.println("low=" + forecast.getForecastLow());
                out.println("pop=" + forecast.getForecastPop());
                out.println("last update=" + forecast.getLastUpdate().getTime() / 1000);
                out.println("last update(human)=" + forecast.getLastUpdate());
            }
        }
    }
}
